package com.jn.mvvm2.views;

import android.content.Intent;
import android.view.View;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.jn.mvvm2.R;
import com.jn.mvvm2.base.BaseActivity;
import com.jn.mvvm2.dao.QAndADao;
import com.jn.mvvm2.databinding.ActivityQandaBinding;
import com.jn.mvvm2.entity.QAndAEntity;
import com.jn.mvvm2.viewmodel.QAndAViewModel;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint // 允许 Hilt 注入依赖
public class QAndAActivity extends BaseActivity<ActivityQandaBinding> {
    private QAndAViewModel mQAndAViewModel;
    private LiveData<QAndAEntity> mQAndALiveData;

    @Inject
    QAndADao qAndADao;

    @Override
    public int getLayoutId() {
        return R.layout.activity_qanda;
    }

    @Override
    public void init() {
        mQAndAViewModel = new ViewModelProvider(this).get(QAndAViewModel.class);
        mQAndALiveData = mQAndAViewModel.getLiveData();
        mQAndALiveData.observe(this, new Observer<QAndAEntity>() {
            @Override
            public void onChanged(QAndAEntity qAndAEntity) {
                mBinding.setData(qAndAEntity);
                qAndADao.insert(qAndAEntity);
            }
        });
        mBinding.setListener(new ButtonClickListener());
    }

    public class ButtonClickListener {
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.button) {
                mQAndAViewModel.getData();
            } else if (id == R.id.to_list) {
                Intent intent = new Intent(QAndAActivity.this, QAndAListActivity.class);
                startActivity(intent);
            }
        }
    }

}
